package com.cfpl.admin_service.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cfpl.admin_service.VO.AvailablePesticide;
import com.cfpl.admin_service.VO.PageQueryCondition;
import com.cfpl.admin_service.VO.PesticideDetails;
import com.cfpl.admin_service.service.PesticideAcquireRecordService;
import com.cfpl.admin_service.service.PesticideTypeService;
import com.cfpl.project_common.pojo.PesticideAcquireRecord;
import com.cfpl.project_common.pojo.PesticideType;
import com.cfpl.project_common.pojo.PesticideUsageRecord;
import com.cfpl.project_common.result.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: wrp
 * @Date: 2023/10/19 - 10:44
 * @Description: com.cfpl.admin_service.controller
 * @version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/PesType")
@Api(tags="农药种类相关接口")
public class PesticideTypeController{

    @Autowired
    private PesticideTypeService pesTypeService;

    @Autowired
    private PesticideAcquireRecordService pesAcqRecordService;

    /**
     * 功能描述：分页查询农药种类，可以按照条件模糊查询
     * \@param currentPage  当前页
     * @param pageSize  每页条数
     * @param condition  查询条件
     * @return  分页对象
     */
    @ApiOperation("分页查询农药种类")
    @GetMapping("/queryAllPesType")
    public BaseResult<PageQueryCondition> queryAll(int currentPage,int pageSize,String condition) {
        Page<PesticideType> page = pesTypeService.querypage(currentPage,pageSize,condition);
        PageQueryCondition queryCondition = new PageQueryCondition(page,condition);
        return BaseResult.ok(queryCondition);
    }

    /**
     * 功能描述：新增一条农药种类对象
     * @param pesType 农药种类对象
     * @return
     */
    @ApiOperation("新增农药种类")
    @PostMapping("/addPesType")
    public BaseResult<String> addPesType(@RequestBody PesticideType pesType) {
        pesTypeService.addPesType(pesType);
        return BaseResult.ok("add success");
    }

    /**
     * 功能描述：根据id查询农药种类对象
     *
     * @param id 农药种类ID
     * @return 农药种类对象
     */
    @ApiOperation("查询一条农药种类")
    @GetMapping("/queryPesTypeById/{id}")
    public BaseResult<PesticideType> queryPesTypeById(@PathVariable("id") Long id) {
        PesticideType pesType = pesTypeService.queryPesTypeById(id);
        return BaseResult.ok(pesType);
    }

    /**
     * 功能描述：修改指定的农药种类对象
     * @param pesType 农药种类对象
     * @return
     */
    @ApiOperation("修改农药种类对象")
    @PutMapping("/modifyPesType")
    public BaseResult modifyPesType(@RequestBody PesticideType pesType) {
        pesTypeService.updatePesType(pesType);
        return BaseResult.ok(pesType);
    }

    /**
     * 功能描述：根据id删除农药种类
     * @param id 农药ID
     * @return
     */
    @ApiOperation("根据id删除农药种类")
    @DeleteMapping("/deletePesTypeById/{id}")
    public BaseResult<String> deletePesTypeById(@PathVariable("id")Long id) {
        PesticideType pesType = pesTypeService.queryPesTypeById(id);
        if (pesType != null) {
            pesTypeService.removePesTypeById(id);
        }
        return BaseResult.ok("delete success");
    }

    /**
     * 功能描述：批量删除农药种类
     * @param Ids 多个农药种类ID
     * @return
     */
    @ApiOperation("批量删除农药种类")
    @DeleteMapping("/deletePesTypeList")
    public BaseResult<String> deletePesTypeList(@RequestParam("Ids")List<Long> Ids) {
        pesTypeService.deleteBatchByIds(Ids);
        return BaseResult.ok("delete success");
    }
    /**
     * 添加农药领取记录
     * @param pesAcqRecord 领取记录
     */
    @ApiOperation("添加农药领取记录")
    @PostMapping("/addPesticideAcquireRecord")
    public BaseResult addPesticideAcquireRecord(@RequestBody PesticideAcquireRecord pesAcqRecord) {
        pesTypeService.addPesticideAcquireRecord(pesAcqRecord);
        return BaseResult.ok();
    }
    /**
     * 添加农药使用记录
     * @param pesUsaRecord 使用记录
     */
    @ApiOperation("添加农药使用记录")
    @PostMapping("/addPesticideUsageRecord")
    public BaseResult addPesticideUsageRecord(@RequestBody PesticideUsageRecord pesUsaRecord) {
        pesTypeService.addPesticideUsageRecord(pesUsaRecord);
        return BaseResult.ok();
    }
    /**
     * 功能描述：根据农药领取记录查询本次的所有施农药记录
     * @param id 农药领取记录id
     * @return 施农药记录
     */
    @ApiOperation("根据农药领取记录查询本次的所有施农药记录")
    @GetMapping("/queryUsageRecordByAcquireRecord/{id}")
    public BaseResult queryUsageRecordByAcquireRecord(@PathVariable("id") Long id){
       List<PesticideDetails> detailsList = pesAcqRecordService.queryUsageRecordByAcquireRecord(id);
       return BaseResult.ok(detailsList);
    }

    /**
     * 功能描述：查询当前施农药人员可使用的农药信息
     * @param adminId 施农药人员
     * @return 农药信息
     */
    @ApiOperation("查询当前施农药人员可使用的农药信息接口")
    @GetMapping("/queryAvailablePesticideList/{adminId}")
    public BaseResult<List<AvailablePesticide>> queryAvailablePesticideList(@PathVariable("adminId") Long adminId){
        List<AvailablePesticide> pesticideList = pesAcqRecordService.queryAvailablePesticideList(adminId);
        return BaseResult.ok(pesticideList);
    }
}
